/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { X2DBeta } = require("../graphics/X2DInterface");

class XButton {
    static STYLE = {
        SINGLE: 0,
        LINK: 1
    }
    constructor(options = null) {
        this.move(0, 0, 0, 0);
        this.name_ = "";
        this.style_ = XButton.STYLE.SINGLE;
        this.touchDown_ = false;
        this.clicked_ = false;
        this.disable_ = false;
        this.textSize_ = 14;
        this.clickCallback_ = null;
        this.onDrag_ = null;
        this.draged_ = false;

        if (options) {
            if (options.name) {
                this.name_ = options.name;
            }
            if (options.x) {
                this.posX_ = options.x;
            }
            if (options.y) {
                this.posY_ = options.y;
            }
            if (options.w) {
                this.posW_ = options.w;
            }
            if (options.h) {
                this.posH_ = options.h;
            }
            if (options.style) {
                this.style_ = options.style;
            }
            if (options.onClicked) {
                this.clickCallback_ = options.onClicked;
            }
            if (options.onDraw) {
                this.onDraw = options.onDraw;
            }
            if (options.onDrag) {
                this.onDrag_ = options.onDrag;
            }
        }

        if (options && options.textColor) {
            this.textColor_ = options.textColor;
        } else {
            this.textColor_ = 0xff20f020;
        }

        if (options && options.textSize) {
            this.textSize_ = options.textSize;
        }

        if (options && options.normal) {
            this.normalAnimaId_ = options.normal;
        } else {
            this.normalAnimaId_ = null;
        }

        if (options && options.down) {
            this.downAnimaId_ = options.down;
        } else {
            this.downAnimaId_ = null;
        }

        if (options && options.anima) {
            this.anima_ = options.anima;
        } else {
            this.anima_ = null;
        }

        if (options && options.callback) {
            this.clickCallback_ = options.callback;
        }

        if (options && options.backgroundColor) {
            this.backgroundColor_ = options.backgroundColor;
        } else {
            this.backgroundColor_ = 0xff804050;
        }

        if (options && options.type) {
            this.type_ = options.type;
        } else {
            this.type_ = 'rect';//'circle'
        }

        this.mouseOn_ = false;
    }

    OnClick(cb) {
        this.clickCallback_ = cb;
    }
    centerX() {
        return this.posX_ + this.posW_ / 2;
    }
    centerY() {
        return this.posY_ + this.posH_ / 2;
    }
    rect() {
        return { x: this.posX_, y: this.posY_, w: this.posW_, h: this.posH_ };
    }
    leftX() {
        return this.posX_;
    }
    topY() {
        return this.posY_;
    }
    rightX() {
        return this.posX_ + this.posW_;
    }
    bottomY() {
        return this.posY_ + this.posH_;
    }
    width() {
        return this.posW_;
    }
    height() {
        return this.posH_;
    }
    move(x, y, w = -1, h = -1) {
        this.posX_ = x;
        this.posY_ = y;
        if (w != -1) this.posW_ = w;
        if (h != -1) this.posH_ = h;
        return this;
    }
    DrawCustom(cb) {
        cb(this.name_, this.posX_, this.posY_, this.posW_, this.posH_);
    }
    onDraw(offx = 0, offy = 0) {
        // if (this.anima_ && this.normalAnimaId_) {
        //     // X2DBeta.fillRect(this.posX_, this.posY_, this.posW_, this.posH_, 0x80000000);
        //     if (this.touchDown_ && this.downAnimaId_) {
        //         this.anima_.DrawAnima(this.downAnimaId_, this.posX_, this.posY_, 1, 1, 0, 0xffffffff, -1);
        //     }
        //     else {
        //         this.anima_.DrawAnima(this.normalAnimaId_, this.posX_, this.posY_, 1, 1, 0, 0xffffffff, -1);
        //     }
        //     if (this.name_ && this.name_.length > 0) {
        //         X2DBeta.drawText(this.name_, this.textSize_, this.posX_ + this.posW_ / 2, this.posY_ + this.posH_ / 2, 1, 1, 0, -2, -2, this.textColor_);
        //     }
        //     return;
        // }

        if (this.style_ == XButton.STYLE.SINGLE) {
            let coloroff = 0;
            if (this.touchDown_) coloroff = 0x00202020;

            if (this.type_ == 'rect') {
                X2DBeta.fillRect(this.posX_ + offx, this.posY_ + offy, this.posW_, this.posH_, this.backgroundColor_ - coloroff);
                X2DBeta.drawRect(this.posX_ + offx, this.posY_ + offy, this.posW_, this.posH_, 0xffffffff);
            } else if (this.type_ == 'circle') {
                X2DBeta.Circle(this.posX_ + this.posW_ / 2 + offx, this.posY_ + this.posH_ / 2 + offy, this.posW_ / 2, this.posH_ / 2, 0, 360, this.backgroundColor_ - coloroff);
            }

            if (this.name_ && this.name_.length > 0) {
                X2DBeta.drawText(this.name_, this.textSize_, this.posX_ + this.posW_ / 2 + offx, this.posY_ + this.posH_ / 2 + offy, 1, 1, 0, -2, -2, this.textColor_ - coloroff);
            }
        }
        else if (this.style_ == XButton.STYLE.LINK) {
            let coloroff = 0;
            if (this.touchDown_) coloroff = 0x00202020;
            if (this.mouseOn_) {
                X2DBeta.fillRect(this.posX_ + offx, this.posY_ + offy, this.posW_, this.posH_, 0x50ff0000);
            }
            let w = X2DBeta.drawText(this.name_, this.textSize_, this.posX_ + this.posW_ / 2 + offx, this.posY_ + this.posH_ / 2 + offy, 1, 1, 0, -2, -2, this.textColor_ - coloroff);
            X2DBeta.drawLine(this.posX_ + offx, this.posY_ + this.posH_ + offy, this.posX_ + offx + this.posW_, this.posY_ + this.posH_ + offy, this.textColor_ - coloroff);
            if (this.posW_ < w) {
                this.posW_ = w;
            }
        }
    }

    IsTouchIn(x, y) {
        if (x < this.posX_) return false;
        if (y < this.posY_) return false;
        if (x > this.posX_ + this.posW_) return false;
        if (y > this.posY_ + this.posH_) return false;
        return true;
    }
    onTouch(msg, x, y) {
        this.mouseOn_ = this.IsTouchIn(x, y);
        switch (msg) {
            case 1:
                if (this.mouseOn_) {
                    this.touchDown_ = true;
                    if (this.onDrag_) {
                        this.oldX_ = x;
                        this.oldY_ = y;
                        this.draged_ = false;
                    }
                }
                break;
            case 2:
                if (this.touchDown_ && this.onDrag_) {
                    let dx = x - this.oldX_;
                    let dy = y - this.oldY_;
                    this.onDrag_(dx, dy);
                    this.oldX_ = x;
                    this.oldY_ = y;
                    this.draged_ = true;
                }
                break;
            case 3:
                if (this.touchDown_ && this.mouseOn_ && !this.draged_) {
                    this.clicked_ = true;
                    this.DoClick();
                }
                this.touchDown_ = false;
                break;
        }
        return this.mouseOn_;
    }

    DoClick() {
        if (this.clickCallback_) {
            this.clickCallback_();
        }
    }

    IsClicked() {
        if (this.clicked_) {
            this.clicked_ = false;
            return true;
        }
        return false;
    }

    isOn() {
        return this.mouseOn_;
    }
    isDown() {
        return this.touchDown_;
    }
}

module.exports = {
    XButton
}